package com.qdsg.modulesihealsdk.detectStrategy.strategy.bloodFatStrategy;

import android.content.Context;

import com.example.moduleutils.LogUtil;
import com.google.gson.Gson;
import com.qdsg.modulesihealsdk.detectStrategy.control.StrategyCallback;
import com.qdsg.modulesihealsdk.entity.BloodFatResultEntity;
import com.qdsg.modulesihealsdk.utils.SihealSdkConstant;
import com.qdsg.modulesihealsdk.detectStrategy.control.DetectBusiness;
import com.siheal.device.reader.measure.BloodFatReader;
import com.siheal.device.reader.singlechip.DataChannelReader;
import com.siheal.entitys.device.DeviceOp;
import com.siheal.entitys.measure.BloodFatEntity;
import com.siheal.exception.BasicException;

/**
 * @Description
 * @Author WuJianFeng
 * @Date 2022/11/21 11:53
 */
public class BloodFatStrategy implements DetectBusiness {
    private DataChannelReader dataChannelReader;
    private BloodFatReader bloodFatReader;
    private Context context;
    private BloodFatCallback bloodFatCallback;
    /**
     *
     * haveGetReceive：是否已出值标志，防止出值后还去调用process方法
     */
    private Boolean haveGetReceive = false;
    private int progress = 0;
    @Override
    public void dealDetect(Context context, StrategyCallback strategyCallback) {
        this.context = context;
        haveGetReceive = false;
        if (strategyCallback instanceof BloodFatCallback){
            bloodFatCallback = (BloodFatCallback)strategyCallback;
            bloodFatCallback.detectProgress(0);
            detectBloodFat();
        }
    }

    @Override
    public void stopThread() {
        try {
            if (bloodFatReader != null){
                bloodFatReader.stopRead();
                bloodFatReader = null;
            }
        }catch (Exception e){

        }
        haveGetReceive = false;
        if (bloodFatCallback != null){
            bloodFatCallback = null;
        }

    }

    private void detectBloodFat(){
        dataChannelReader = (new DataChannelReader(SihealSdkConstant.channelID_bloodFat, context){
            // 数据通道切换成功，开始调用血脂读值方法
            @Override
            public void resultReceived(DeviceOp result) {
                bloodFatReader = (new BloodFatReader(context) {
                    //血脂数据获取成功，开始做后续数据处理，当前操作将结果数据回显至H5页面
                    //获取一次结果(最终结果)，在此方法中接收检测结果
                    @Override
                    public void resultReceived(BloodFatEntity result) {
                        String data = new Gson().toJson(result);
                        LogUtil.logAndWrite("__bloodFat-resultReceived",data);

                        //得到数据
                        progress = 0;
                        if (bloodFatCallback != null && !haveGetReceive){
                            bloodFatCallback.detectProgress(100);
                            haveGetReceive = true;
                            bloodFatCallback.resultReceived(new BloodFatResultEntity(result));
                        }
                    }

                    //无需处理
                    @Override
                    public void process(BloodFatEntity middleResult) {
                        String data = new Gson().toJson(middleResult);
                        LogUtil.logAndWrite("__bloodFat-process",data);
                        if (!haveGetReceive && bloodFatCallback != null){
                            bloodFatCallback.process(new BloodFatResultEntity(middleResult));
                        }

                    }

                    //调用过程中出现的异常
                    @Override
                    public void occurError(BasicException e) {
                                   /*
                                    int MEASURE_OUT_RANGE_EXCEPTION = 1001;//测量失败建议重测 temperature <= 32 || temperature >= 42.9
                                    int MEASURE_TIME_OUT = 1005;//测量超时
                                   */
                        String data = new Gson().toJson(e);
                        if (bloodFatCallback != null){
                            LogUtil.logAndWrite("__bloodFat-occurError",data);
                            bloodFatCallback.occurError(e);
                        }
                    }
                });
                if (bloodFatReader != null){
                    bloodFatReader.start();
                }
            }
            //数据通道切换process方法，实现了父类抽象方法，在此无实义
            @Override
            public void process(DeviceOp middleResult) {}
            //数据通道切换异常处理方法
            @Override
            public void occurError(BasicException exception) {
                if (bloodFatCallback != null){
                    bloodFatCallback.occurError(exception);
                }
                /*  int ERROR_IO = 1;     //打开设备" + device.getDeviceID() + "数据通道异常
                            int ERROR_WRITE_DATA = 2;//设备" + device.getDeviceID() + "写入数据发生异常
                            int ERROR_USB_NOT_FOUND = 2001;//没有找到指定地址下(" + device.getPath() + ")设备
                            int ERROR_USB_NOT_SUPPORT = 2002;//本系统不支持USB模块
                            int ERROR_USB_UNKONW_TYPE = 2003;//未知的USB设备类型
                            int ERROR_USB_REQUEST_PERMISSION = 2004;//请求usb权限失败
                             int MEASURE_PROBE_CONFIG_ERROR = 1008;//配置文件配置错误
                            int ERROR_USB_DEVICE_OPEN = 2005;//打开usb设备错误*/
            }
        });
        if (dataChannelReader != null){
            dataChannelReader.start();
        }
    }
}
